package cn.com.huzo.service.master.impl;

import cn.com.huzo.model.entities.master.MaterialGroupMemberMaterialEntity;
import huzofw.service.impl.GenericManagerImpl;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import cn.com.huzo.service.master.MaterialGroupMemberMaterialManager;
import cn.com.huzo.repository.master.MaterialGroupMemberMaterialRepository;
import java.util.HashMap;
import java.util.Map;

/**
 * 材料分组管理服务对象类实现。
 *
 * @author 吴旭
 */
@Service("MaterialGroupMemberMaterialManager")
public class MaterialGroupMemberMaterialManagerImpl extends GenericManagerImpl<MaterialGroupMemberMaterialEntity, HashMap>
        implements MaterialGroupMemberMaterialManager {

    @Resource(name = "MaterialGroupMemberMaterialRepository")
    private MaterialGroupMemberMaterialRepository _dao;

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public List<MaterialGroupMemberMaterialEntity> fetchByMaterialKey(Long materialKey) throws Exception {
        return _dao.fetchByMaterialKey(materialKey);
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public Map<String, Object> batchInsert(List<MaterialGroupMemberMaterialEntity> itemList) throws Exception {
        Map<String, Object> _result = new HashMap();
        Integer rowsAffected = _dao.batchInsert(itemList);
        // 审计日志
        if (rowsAffected > 0) {
            _result.put("Success", true);
            writeAuditLog(itemList.get(0).getCreatedBy(), "基础信息→材料分组设置",
                    "料号：" + itemList.get(0).getMaterialNo(), "增加分组");
        } else {
            _result.put("Success", false);
        }
        _result.put("LinesList", _dao.fetchByMaterialKey(itemList.get(0).getMaterialId()));

        return _result;
    }

    /**
     * {@inheritDoc}
     *
     * @throws java.lang.Exception
     */
    @Transactional
    @Override
    public List<MaterialGroupMemberMaterialEntity> deleteLines(Long groupId, String groupName, Long materialId, String materialName,
            Long deletedBy) throws Exception {
        Map<String, Object> criterion = new HashMap();
        criterion.put("MaterialId", materialId);
        criterion.put("GroupId", groupId);
        Integer rowsAffected = _dao.delete(criterion);
        // 审计日志
        if (rowsAffected > 0) {
            writeAuditLog(deletedBy, "基础信息→材料分组设置",
                    "料名：" + materialName + "　材料组：" + groupName,
                    "删除分组");
        }
        return _dao.fetchByMaterialKey(materialId);
    }
}
